package star.cloud.asme.asmePerson.asmePersonCertification;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import star.cloud.asme.base.dto.Result;
import star.cloud.asme.base.file.fileInfo.FileInfo;
import star.cloud.asme.base.file.fileInfo.FileInfoMapper;
import star.cloud.asme.base.mybatis.IBaseServiceImpl;
import star.cloud.asme.base.utils.ObjectsUtil;
import star.cloud.asme.system.user.User;
import star.cloud.asme.system.user.UserMapper;

import java.util.UUID;


@Service
@Slf4j
@RequiredArgsConstructor
public class AsmePersonCertificationServiceImpl extends IBaseServiceImpl<AsmePersonCertificationMapper, AsmePersonCertification> implements AsmePersonCertificationService {

    private final AsmePersonCertificationMapper asmePersonCertificationMapper;
    private final UserMapper userMapper;
    private final FileInfoMapper fileInfoMapper;

    @Override
    public Result<AsmePersonCertification> getOneByCondition(String columnName, Object columnValue) {

        LambdaQueryWrapper<AsmePersonCertification> queryWrapper = new LambdaQueryWrapper<>();

        switch (columnName) {
            case "id" -> queryWrapper.eq(AsmePersonCertification::getId, columnValue);
            case "username" -> queryWrapper.eq(AsmePersonCertification::getUsername, columnValue);
        }

        AsmePersonCertification asmePersonCertification = asmePersonCertificationMapper.selectOne(queryWrapper);

        User user = userMapper.selectOneByUsername(asmePersonCertification.getUsername());

        setCreatorAndUpdater(asmePersonCertification);

        asmePersonCertification.setUser(user);

        return Result.ok(asmePersonCertification);
    }

    @Override
    public Result<IPage<AsmePersonCertification>> getListByConditionsWithPage(AsmePersonCertification conditions) {

        IPage<AsmePersonCertification> iPage = new Page<>(conditions.getCurrentPage(), conditions.getPageSize());

        LambdaQueryWrapper<AsmePersonCertification> queryWrapper = new LambdaQueryWrapper<>();

        asmePersonCertificationMapper.selectPage(iPage, queryWrapper);

        iPage.getRecords().forEach(asmePersonCertification -> {
            setCreatorAndUpdater(asmePersonCertification);
            FileInfo fileInfo = fileInfoMapper.selectOneByKeyWord(asmePersonCertification.getAttachmentUuid());
            if (fileInfo != null) {
                asmePersonCertification.setAttachmentUrl(fileInfo.getUrl());
            }
        });


        return Result.ok(iPage);

    }

    @Override
    public Result<AsmePersonCertification> insertOne(AsmePersonCertification entity) {
        User user = userMapper.selectOneByUsername(entity.getUsername());
        entity.setName(user.getName());
        entity.setWorkNo(user.getWorkNo());
        entity.setAttachmentUuid(UUID.randomUUID().toString());
        if (ObjectsUtil.isNullOrEmpty(entity.getCertificationReference())) {
            setCertificationRef(entity);
        }
        int inserted = asmePersonCertificationMapper.insert(entity);
        entity.setUser(user);
        setCreatorAndUpdater(entity);
        return inserted > 0 ? Result.ok(entity) : Result.error();
    }

    @Override
    public Result<AsmePersonCertification> updateOne(AsmePersonCertification entity) {
        setVersion(entity);
        asmePersonCertificationMapper.updateById(entity);
        User user = userMapper.selectOneByUsername(entity.getUsername());
        entity.setUser(user);
        setCreatorAndUpdater(entity);
        return Result.ok(entity);
    }


    private void setCertificationRef(AsmePersonCertification asmePersonCertification) {
        String reference = "ASME-";
        reference += asmePersonCertification.getDomain().toUpperCase() + "-";
        reference += asmePersonCertification.getStartDate().getYear() + "-";
        reference += asmePersonCertification.getWorkNo() + "-";
        reference += asmePersonCertification.getMethod().toUpperCase();
        asmePersonCertification.setCertificationReference(reference);
    }

}
